package com.dmall.algorithm.chapter03;

import java.util.Arrays;

/**
 * @author xiao1.wang
 * @date 2020-06-30 18:05:17
 */
public class Leetcode27 {

    public int removeElement(int[] nums, int val) {
        int res = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != val) {
                if (res != i) {
                    swap(nums, res, i);
                }
                res ++;
            }
        }
        return res;
    }

    private void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

    public static void main(String[] args) {
        int[] nums = {6, 3, 2, 2, 3, 2, 4, 3, 5};
        Leetcode27 leetcode27 = new Leetcode27();
        int res = leetcode27.removeElement(nums, 3);
        Arrays.stream(nums).forEach(System.out::println);
        System.out.println();
        System.out.println(res);
    }
}
